#include "advancedConfig.h"
#include "ui_advancedConfig.h"



AdvancedConfig::AdvancedConfig(int row, int col, QWidget *parent) :
    QWidget(parent),
    ui(new Ui::AdvancedConfig), _row(row), _col(col)
{
    ui->setupUi(this);

    _posiPannel = new PosiPannel(row, col, 1);
    _posiPannel->setNormMode(true);
    ui->layout_posiPannel->addWidget(_posiPannel);
    ui->layout_selectType->addSpacerItem(new QSpacerItem(20,20,QSizePolicy::Expanding));
    ui->radioButton_FAM->setChecked(true);

    _buttonGroup_channel = new QButtonGroup(this);
    _buttonGroup_fixedBaseline = new QButtonGroup(this);
    _buttonGroup_channel->addButton(ui->radioButton_FAM);
    _buttonGroup_channel->addButton(ui->radioButton_CY5);
    _buttonGroup_channel->addButton(ui->radioButton_ROX);
    _buttonGroup_channel->addButton(ui->radioButton_VIC);
    _buttonGroup_fixedBaseline->addButton(ui->radioButton_open);
    _buttonGroup_fixedBaseline->addButton(ui->radioButton_close);

    connect(_posiPannel, SIGNAL(sigSelectChanged(QStringList)), this, SLOT(on_selectPosiChanged(QStringList)));


    _defaultConfig["FIXED_BASE_END"] = -1;
    _defaultConfig["CLIP_START"] =     5;
    _defaultConfig["SLOPE_THR"] =      2.8;
    _defaultConfig["SLOPE_THR_DOWN"] = 0.6;
    _defaultConfig["MIN_CONF"] =       0.9;
    _defaultConfig["RANGE_THR"] =      1000;

    ui->pushButton_single->hide();
    ui->pushButton_channel->hide();

}

void AdvancedConfig::showEvent(QShowEvent *event){
    _curChannel = "FAM";
    ui->radioButton_FAM->setChecked(true);
    if(_selectableList.size()==0){
        qDebug() << "数据列表为空，无法高级配置";
        return;
    }
    _curPosi = QStringList();
    _posiPannel->setSelectColor(QColor("#93A8FC"));
    _posiPannel->setSelectCell(QStringList(_curPosi));
    state2edit();
}

AdvancedConfig::~AdvancedConfig()
{
    delete ui;
}



void AdvancedConfig::setParamsList(QMap<QString, QMap<QString, double>> states){
    _states = states;
}
void AdvancedConfig::setSelectableList(QStringList list){
    _selectableList = list;
    updatePannelSelectable();
}

void AdvancedConfig::setFittedList(QVector<QStringList> list){
    _fittedLists = list;
    updatePannelSelectable();
}

void AdvancedConfig::updatePannelSelectable(){

    QStringList posiList = _selectableList;
//    foreach(QString curve, _selectableList){          // selectable细化暂时关闭
//        if(curve.contains(_curChannel)){
//            posiList.push_back(curve.split('_')[1]);
//        }
//    }
    _posiPannel->setSelectableCell(posiList);
    foreach(QString posi, _curPosi){
        if(!posiList.contains(posi)){
            _curPosi.removeAt(_curPosi.indexOf(posi));
        }
    }
//    _curPosi = QStringList();

    if(_curChannel=="FAM"){
        _posiPannel->setSelectColor(QColor("#93A8FC"));
        if(_fittedLists.size() >= 1){
            _posiPannel->setNormCell(_fittedLists[0]);
        }
    }else if(_curChannel=="VIC"){
        _posiPannel->setSelectColor(QColor("#82F0AF"));
        if(_fittedLists.size() >= 2){
            _posiPannel->setNormCell(_fittedLists[1]);
        }
    }else if(_curChannel=="ROX"){
        _posiPannel->setSelectColor(QColor("#FFC39A"));
        if(_fittedLists.size() >= 3){
            _posiPannel->setNormCell(_fittedLists[2]);
        }
    }else if(_curChannel=="CY5"){
        _posiPannel->setSelectColor(QColor("#FF8387"));
        if(_fittedLists.size() >= 4){
            _posiPannel->setNormCell(_fittedLists[3]);
        }
    }

}

void AdvancedConfig::on_selectPosiChanged(QStringList str){
    if(str.size() < 1){
        return;
    }
    _curPosi = str;
    emit sigSelectCurveChanged(getSelectedNameList(str));

    state2edit();
}

void AdvancedConfig::edit2state(){

    foreach(QString posi, _curPosi){

        QMap<QString, double> res;
        res["FIXED_BASE_END"] = ui->radioButton_open->isChecked() ? ui->lineEdit_FIXED_BASE_END->text().toDouble() : -1;
        res["CLIP_START"] =     ui->lineEdit_CLIP_START->text().toDouble();
        res["SLOPE_THR"] =      ui->lineEdit_SLOPE_THR->text().toDouble();
        res["SLOPE_THR_DOWN"] = ui->lineEdit_SLOPE_THR_DOWN->text().toDouble();
        res["MIN_CONF"] =       ui->lineEdit_MIN_CONF->text().toDouble();
        res["RANGE_THR"] =      ui->lineEdit_RANGE_THR->text().toDouble();
        _states[_curChannel+"_"+posi] = res;
    }


}


QStringList AdvancedConfig::getSelectedNameList(QStringList l){

    QStringList list;
    foreach(QString s, l){
        list.push_back(_curChannel + "_" + s);
    }
    return list;
}

void AdvancedConfig::state2edit(){

    QMap<QString, double> res;
    if(_curPosi.size() == 1){
        res = _states[_curChannel+"_"+_curPosi[0]];
    }else{
        res = _defaultConfig;
    }
    ui->radioButton_open->setChecked(res["FIXED_BASE_END"]!=-1);
    ui->radioButton_close->setChecked(res["FIXED_BASE_END"]==-1);
    ui->lineEdit_FIXED_BASE_END  ->setText(QString::number(res["FIXED_BASE_END"]));
    ui->lineEdit_CLIP_START      ->setText(QString::number(res["CLIP_START"]));
    ui->lineEdit_SLOPE_THR       ->setText(QString::number(res["SLOPE_THR"]));
    ui->lineEdit_SLOPE_THR_DOWN  ->setText(QString::number(res["SLOPE_THR_DOWN"]));
    ui->lineEdit_MIN_CONF        ->setText(QString::number(res["MIN_CONF"]));
    ui->lineEdit_RANGE_THR       ->setText(QString::number(res["RANGE_THR"]));
    updateEditorState();

}

void AdvancedConfig::on_radioButton_open_clicked(){
    ui->lineEdit_FIXED_BASE_END->setText("20");
    this->updateEditorState();
}void AdvancedConfig::on_radioButton_close_clicked(){
    ui->lineEdit_FIXED_BASE_END->setText("-1");
    this->updateEditorState();
}

void AdvancedConfig::updateEditorState(){
    if(ui->radioButton_open->isChecked()){
        ui->lineEdit_FIXED_BASE_END ->setEnabled(true);
        ui->lineEdit_MIN_CONF       ->setEnabled(false);
        ui->lineEdit_RANGE_THR      ->setEnabled(false);
        ui->lineEdit_SLOPE_THR      ->setEnabled(false);
        ui->lineEdit_CLIP_START     ->setEnabled(false);
        ui->lineEdit_SLOPE_THR_DOWN ->setEnabled(false);
    }else if(ui->radioButton_close->isChecked()){
        ui->lineEdit_FIXED_BASE_END ->setEnabled(false);
        ui->lineEdit_MIN_CONF       ->setEnabled(true);
        ui->lineEdit_RANGE_THR      ->setEnabled(true);
        ui->lineEdit_SLOPE_THR      ->setEnabled(true);
        ui->lineEdit_CLIP_START     ->setEnabled(true);
        ui->lineEdit_SLOPE_THR_DOWN ->setEnabled(true);
    }
}

void AdvancedConfig::on_radioButton_CY5_clicked(){
    _curChannel = "CY5";
    updatePannelSelectable();
    emit sigSelectCurveChanged(getSelectedNameList(_curPosi));
    state2edit();
}void AdvancedConfig::on_radioButton_FAM_clicked(){
    _curChannel = "FAM";
    updatePannelSelectable();
    emit sigSelectCurveChanged(getSelectedNameList(_curPosi));
    state2edit();
}void AdvancedConfig::on_radioButton_ROX_clicked(){
    _curChannel = "ROX";
    updatePannelSelectable();
    emit sigSelectCurveChanged(getSelectedNameList(_curPosi));
    state2edit();
}void AdvancedConfig::on_radioButton_VIC_clicked(){
    _curChannel = "VIC";
    updatePannelSelectable();
    emit sigSelectCurveChanged(getSelectedNameList(_curPosi));
    state2edit();
}

void AdvancedConfig::on_pushButton_reanalysis_clicked()
{
    if(_curPosi.size() <= 0){
        return;
    }
    edit2state();
    emit sigReanalysis(getSelectedNameList(_curPosi), _states[_curChannel+"_"+_curPosi[0]]);
}

void AdvancedConfig::on_pushButton_setDefault_clicked(){

    QMap<QString, double> res;
    res["FIXED_BASE_END"] = -1;
    res["CLIP_START"] =     5;
    res["SLOPE_THR"] =      2.8;
    res["SLOPE_THR_DOWN"] = 0.6;
    res["MIN_CONF"] =       0.9;
    res["RANGE_THR"] =      1000;

    ui->radioButton_open->setChecked(res["FIXED_BASE_END"]!=-1);
    ui->radioButton_close->setChecked(res["FIXED_BASE_END"]==-1);
    ui->lineEdit_FIXED_BASE_END  ->setText(QString::number(res["FIXED_BASE_END"]));
    ui->lineEdit_CLIP_START      ->setText(QString::number(res["CLIP_START"]));
    ui->lineEdit_SLOPE_THR       ->setText(QString::number(res["SLOPE_THR"]));
    ui->lineEdit_SLOPE_THR_DOWN  ->setText(QString::number(res["SLOPE_THR_DOWN"]));
    ui->lineEdit_MIN_CONF        ->setText(QString::number(res["MIN_CONF"]));
    ui->lineEdit_RANGE_THR       ->setText(QString::number(res["RANGE_THR"]));
}

void AdvancedConfig::on_pushButton_single_clicked(){
    _posiPannel->show();
    ui->pushButton_channel->setStyleSheet("background: #FFFFFF;\n\nborder: 1px solid #007bd8;");
    ui->pushButton_single->setStyleSheet("background: #007bd8; color: #FFFFFF;\n\nborder: 1px solid #007bd8;");
    state2edit();
}

void AdvancedConfig::on_pushButton_channel_clicked()
{
    _posiPannel->hide();
    ui->pushButton_single->setStyleSheet("background: #FFFFFF;\n\nborder: 1px solid #007bd8;");
    ui->pushButton_channel->setStyleSheet("background: #007bd8; color: #FFFFFF;\n\nborder: 1px solid #007bd8;");
    state2edit();
}

void AdvancedConfig::on_pushButton_cancel_clicked()
{
    emit sigCancel();
}

void AdvancedConfig::on_pushButton_clear_clicked()
{
    _posiPannel->setSelectCell(QStringList());
    _curPosi = QStringList();
}
